package com.sree.tali.actions;

import com.sree.tali.buscomp.AdminBusComp;
import com.sree.tali.buscomp.AdminBusCompImpl;
import com.sree.tali.common.CommonUtils;
import com.sree.tali.plugin.HibernatePlugin;
import com.sree.tali.pojo.AdminHomepojo;
import com.sree.tali.pojo.UploadImagePojo;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.SessionFactory;

public class UploadImages
  extends DispatchAction
{
  private static final String SUCCESS = "success";
  
  public ActionForward uploadImagePage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    String basepath = getServlet().getServletContext().getRealPath("/");
    List<String> list = new ArrayList();
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(getServlet().getServletContext().getRealPath("/") + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setFolders(list);
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward getSubfolders(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
    String basepath = getServlet().getServletContext().getRealPath("/") + uploadimagepojo.getFolderName();
    List<String> list = new ArrayList();
    String[] children = new File(basepath).list();
    for (String subdir : children) {
      if (new File(basepath + "\\" + subdir).isDirectory()) {
        list.add(subdir);
      }
    }
    uploadimagepojo.setSubFolders(list);
    return mapping.findForward("uloadimages");
  }
  
  public ActionForward uploadImages(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
    /*UploadImagePojo uploadimagepojo = (UploadImagePojo)form;
   System.out.println("images are ==== " + uploadimagepojo.getImageList().size());
    
    return mapping.findForward("uloadimages");*/
      AdminBusComp buscomp = new AdminBusCompImpl();
    UploadImagePojo localAdminHomepojo = (UploadImagePojo)form;
     CommonUtils localCommonUtils = new CommonUtils();
    SessionFactory localSessionFactory = (SessionFactory)this.servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);
    //localAdminHomepojo.setFilPath(getServlet().getServletContext().getRealPath("/") + localAdminHomepojo.getUploadType().toUpperCase() + "/" + localAdminHomepojo.getFolderName());
    //this.buscomp.UploadHomeAction(localAdminHomepojo, localSessionFactory.openSession());
    String temp=localAdminHomepojo.getFolderName()+"/"+localAdminHomepojo.getSubfolderName()+"/"+localAdminHomepojo.getDirectoryname();
    
    System.out.println(temp);
    String path = temp.replace("/null", "").replaceAll("\\s+","");
    System.out.println(path);
    String str = getServlet().getServletContext().getRealPath("/")+path;
    localCommonUtils.UploadFile(localAdminHomepojo.getImage(), str);
    if(localAdminHomepojo.getSubfolderName()!=null)
    {
         setImageCountinContext(servlet, localAdminHomepojo.getSubfolderName());
    }
    return mapping.findForward("uloadimages");
  }
  
  public void setImageCountinContext(ActionServlet paramActionServlet, String folder)
  {
      String folder1= paramActionServlet.getServletContext().getRealPath("/")+"topimages/"+folder+"/images";
      System.out.println("==========================="+folder1);
      int count = new File(folder1).list().length;
      System.out.println("================================="+count);
      paramActionServlet.getServletContext().setAttribute(folder, count);
  }
  
  
}



